package com.ruoyi.web.controller.test;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.service.FinancialDataService;
import com.ruoyi.common.utils.AllTickApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * API测试控制器
 * 用于测试各种外部API的连接状态
 */
@RestController
@RequestMapping("/test/api")
public class ApiTestController {
    
    @Autowired
    private FinancialDataService financialDataService;

    /**
     * 测试UniRateAPI连接
     */
    @GetMapping("/unirate")
    public AjaxResult testUniRateApi() {
        try {
            String result = AllTickApiUtil.testUniRateApiConnection();
            return AjaxResult.success("UniRateAPI连接测试成功", result);
        } catch (Exception e) {
            return AjaxResult.error("UniRateAPI连接测试失败: " + e.getMessage());
        }
    }

    /**
     * 测试外汇数据获取
     */
    @GetMapping("/forex")
    public AjaxResult testForexData() {
        try {
            String eurusd = AllTickApiUtil.getForexQuote("EURUSD");
            String gbpusd = AllTickApiUtil.getForexQuote("GBPUSD");
            String usdjpy = AllTickApiUtil.getForexQuote("USDJPY");
            String usdchf = AllTickApiUtil.getForexQuote("USDCHF");
            String audusd = AllTickApiUtil.getForexQuote("AUDUSD");
            String nzdusd = AllTickApiUtil.getForexQuote("NZDUSD");
            String usdcad = AllTickApiUtil.getForexQuote("USDCAD");
            
            return AjaxResult.success("外汇数据获取成功")
                    .put("EURUSD", eurusd)
                    .put("GBPUSD", gbpusd)
                    .put("USDJPY", usdjpy)
                    .put("USDCHF", usdchf)
                    .put("AUDUSD", audusd)
                    .put("NZDUSD", nzdusd)
                    .put("USDCAD", usdcad);
        } catch (Exception e) {
            return AjaxResult.error("外汇数据获取失败: " + e.getMessage());
        }
    }

    /**
     * 测试黄金数据获取
     */
    @GetMapping("/gold")
    public AjaxResult testGoldData() {
        try {
            String gold = AllTickApiUtil.getMetalQuote("GOLD");
            String silver = AllTickApiUtil.getMetalQuote("SILVER");
            String xau = AllTickApiUtil.getMetalQuote("XAU");
            String xag = AllTickApiUtil.getMetalQuote("XAG");
            return AjaxResult.success("贵金属数据获取成功")
                    .put("GOLD", gold)
                    .put("SILVER", silver)
                    .put("XAU", xau)
                    .put("XAG", xag);
        } catch (Exception e) {
            return AjaxResult.error("贵金属数据获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试原始API数据
     */
    @GetMapping("/raw")
    public AjaxResult testRawApiData() {
        try {
            String result = AllTickApiUtil.testUniRateApiConnection();
            // 解析并显示XAU和XAG的原始数据
            return AjaxResult.success("原始API数据获取成功", result);
        } catch (Exception e) {
            return AjaxResult.error("原始API数据获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试统一金融数据服务
     */
    @GetMapping("/financial/all")
    public AjaxResult testFinancialDataService() {
        try {
            Map<String, String> allData = financialDataService.getAllFinancialData();
            return AjaxResult.success("统一金融数据服务测试成功", allData);
        } catch (Exception e) {
            return AjaxResult.error("统一金融数据服务测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试外汇数据（统一服务）
     */
    @GetMapping("/financial/forex")
    public AjaxResult testUnifiedForexData() {
        try {
            Map<String, String> forexData = financialDataService.getForexData();
            return AjaxResult.success("外汇数据获取成功", forexData);
        } catch (Exception e) {
            return AjaxResult.error("外汇数据获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试贵金属数据
     */
    @GetMapping("/financial/metal")
    public AjaxResult testMetalData() {
        try {
            Map<String, String> metalData = financialDataService.getMetalData();
            return AjaxResult.success("贵金属数据获取成功", metalData);
        } catch (Exception e) {
            return AjaxResult.error("贵金属数据获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试商品数据（已禁用）
     */
    @GetMapping("/financial/commodity")
    public AjaxResult testCommodityData() {
        try {
            Map<String, String> commodityData = financialDataService.getCommodityData();
            return AjaxResult.success("商品数据已禁用，返回空数据", commodityData);
        } catch (Exception e) {
            return AjaxResult.error("商品数据获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取缓存状态
     */
    @GetMapping("/financial/cache/status")
    public AjaxResult getCacheStatus() {
        try {
            Map<String, Object> status = financialDataService.getCacheStatus();
            return AjaxResult.success("缓存状态获取成功", status);
        } catch (Exception e) {
            return AjaxResult.error("缓存状态获取失败: " + e.getMessage());
        }
    }
    
    /**
     * 手动刷新缓存
     */
    @GetMapping("/financial/cache/refresh")
    public AjaxResult refreshCache() {
        try {
            financialDataService.refreshCache();
            return AjaxResult.success("缓存刷新成功");
        } catch (Exception e) {
            return AjaxResult.error("缓存刷新失败: " + e.getMessage());
        }
    }
    
    /**
     * 清除所有缓存
     */
    @GetMapping("/financial/cache/clear")
    public AjaxResult clearCache() {
        try {
            financialDataService.clearAllCache();
            return AjaxResult.success("缓存清除成功");
        } catch (Exception e) {
            return AjaxResult.error("缓存清除失败: " + e.getMessage());
        }
    }
} 